package com.guet.controller.user;

import com.guet.DTO.PaperPageDTO;
import com.guet.DTO.QuestionsSaveDTO;
import com.guet.VO.CorrectAnswerVO;
import com.guet.VO.QuestionsForStuVO;
import com.guet.result.PageResult;
import com.guet.result.Result;
import com.guet.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController("studentTestController")
@RequestMapping("student/test")
@CrossOrigin
public class TestController {
    @Autowired
    private TestService testService;

    /**
     * 分页查询试卷
     *
     * @param paperPageDTO
     * @return
     */
    @PostMapping("paper")
    public Result<PageResult> getPaperList(@RequestBody PaperPageDTO paperPageDTO) {
        log.info("paperPageDTO:{}", paperPageDTO);
        PageResult pageResult = testService.getPaperList(paperPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据试卷id获取题目
     *
     * @param id
     * @return
     */
    @GetMapping("question/{id}")
    public Result<List<QuestionsForStuVO>> getQuestionsForStuList(@PathVariable Integer id) {
        log.info("id:{}", id);
        List<QuestionsForStuVO> result = testService.getQuestionList(id);
        return Result.success(result);
    }

    /**
     * 保存学生作答的试卷
     *
     * @param questionsSaveDTO
     * @return
     */
    @PostMapping("question/save")
    public Result<Object> saveQuestion(@RequestBody QuestionsSaveDTO questionsSaveDTO, Integer paperId) {
        log.info("questionsSaveDTO:{}", questionsSaveDTO);
        testService.saveAnswer(questionsSaveDTO, paperId);
        return Result.success();
    }

    /**
     * 回显学生的试题作答
     *
     * @param paperId
     * @return
     */
    @GetMapping("question/answer/{paperId}")
    public Result<Map<Integer, String>> getAnswer(@PathVariable Integer paperId) {
        log.info("paperId:{}", paperId);
        Map<Integer, String> result = testService.getAnswer(paperId);
        return Result.success(result);
    }

    @PutMapping("question/answer/{paperId}")
    public Result<Object> submit(@PathVariable Integer paperId) {
        log.info("paperId:{}", paperId);
        testService.computeScore(paperId);
        return Result.success();
    }

    /**
     * 根据试卷id查询没到题目的正确选项的解析
     * @param id
     * @return
     */
    @GetMapping("paper/correctAnswer/{id}")
    public Result<List<CorrectAnswerVO>> getCorrectAnswer(@PathVariable Integer id){
        log.info("paper:{}", id);
        List<CorrectAnswerVO> correctAnswerVOs = testService.getCorrectAnswer(id);
        return Result.success(correctAnswerVOs);
    }

    /**
     * 获取学生分数
     * @param id
     * @return
     */
    @GetMapping("paper/score/{id}")
    public Result<String> getScore(@PathVariable Integer id){
        log.info("id:{}", id);
        String score = testService.getScore(id);
        return Result.success(score);
    }
}
